package com.zlx.mall.basic.web.service;


import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientResponseException;

/**
 * @author qingke
 **/
@Slf4j(topic = "httpServiceLogger")
@Service
public class HttpLogServiceImpl implements IHttpLogService {
    @Override
    public void safeSaveLog(String code, Long requestTimestamp, RequestEntity<?> requestEntity, Long responseTimestamp, @Nullable ResponseEntity<?> responseEntity) {

    }

    @Override
    public void safeSaveLog(String code, Long requestTimestamp, RequestEntity<?> requestEntity, Long responseTimestamp, RestClientResponseException restClientResponseException) {

    }

    /*@Resource
    private IBasicHttpTraceLogService httpTraceLogService;
    @Resource(name = "requiresNewTransactionTemplate")
    private TransactionTemplate requiresNewTransactionTemplate;

    protected String getHeaders(HttpEntity<?> httpEntity) {
        return Optional
                .ofNullable(httpEntity)
                .map(HttpEntity::getHeaders)
                .map(JacksonUtil::toJsonString)
                .orElse(StringUtils.EMPTY);
    }

    protected String getBody(HttpEntity<?> httpEntity) {
        return Optional
                .ofNullable(httpEntity)
                .map(HttpEntity::getBody)
                .map(JacksonUtil::toJsonString)
                .orElse(StringUtils.EMPTY);
    }

    @Override
    public void safeSaveLog(String code,
                            Long requestTimestamp,
                            RequestEntity<?> requestEntity,
                            Long responseTimestamp,
                            @Nullable ResponseEntity<?> responseEntity) {

        requiresNewTransactionTemplate.executeWithoutResult(status -> {
            try {
                BasicHttpTraceLog trace = new BasicHttpTraceLog();
                trace.setTraceId(MDCUtil.getOrCreateTraceId());
                trace.setGroupName(code);

                trace.setRequestTimestamp(requestTimestamp);
                trace.setRequestMethod(Optional
                        .ofNullable(requestEntity.getMethod())
                        .map(Enum::name)
                        .orElse(StringUtils.EMPTY));
                trace.setRequestUrl(requestEntity.getUrl().toString());
                trace.setRequestHeaders(getHeaders(requestEntity));
                trace.setRequestBody(getBody(requestEntity));

                trace.setResponseTimestamp(responseTimestamp);

                trace.setResponseStatus(Optional
                        .ofNullable(responseEntity)
                        .map(ResponseEntity::getStatusCodeValue)
                        .orElse(null));
                trace.setResponseHeaders(getHeaders(responseEntity));
                trace.setResponseBody(getBody(responseEntity));
                trace.setDurationMillis(Optional
                        .of(responseTimestamp)
                        .map(timestamp -> timestamp - requestTimestamp)
                        .orElse(null));
                trace.setCreateTimestamp(Instant.now());

                log.info("Http Log Service: {}", JacksonUtil.toJsonString(trace));

                if (!httpTraceLogService.save(trace)) {
                    throw new IllegalBizStatusException("Save Http Log Failed");
                }

            } catch (Exception e) {
                log.error("### save request log failed, RequestEntity: {}, ResponseEntity: {}",
                        requestEntity, responseEntity, e);
            }
        });
    }

    @Override
    public void safeSaveLog(String code,
                            Long requestTimestamp,
                            RequestEntity<?> requestEntity,
                            Long responseTimestamp,
                            RestClientResponseException restClientResponseException) {
        requiresNewTransactionTemplate.executeWithoutResult(status -> {
            try {
                BasicHttpTraceLog trace = new BasicHttpTraceLog();
                trace.setTraceId(MDCUtil.getOrCreateTraceId());
                trace.setGroupName(code);

                trace.setRequestTimestamp(requestTimestamp);
                trace.setRequestMethod(Optional
                        .ofNullable(requestEntity.getMethod())
                        .map(Enum::name)
                        .orElse(StringUtils.EMPTY));
                trace.setRequestUrl(requestEntity.getUrl().toString());
                trace.setRequestHeaders(getHeaders(requestEntity));
                trace.setRequestBody(getBody(requestEntity));

                trace.setResponseTimestamp(responseTimestamp);

                trace.setResponseStatus(restClientResponseException.getRawStatusCode());
                trace.setResponseHeaders(Optional
                        .ofNullable(restClientResponseException.getResponseHeaders())
                        .map(JacksonUtil::toJsonString)
                        .orElse(StringUtils.EMPTY));
                trace.setResponseBody(restClientResponseException.getResponseBodyAsString());

                trace.setDurationMillis(Optional
                        .of(responseTimestamp)
                        .map(timestamp -> timestamp - requestTimestamp)
                        .orElse(null));

                log.info("Http Log Service: {}", JacksonUtil.toJsonString(trace));

                if (!httpTraceLogService.save(trace)) {
                    throw new IllegalBizStatusException("Save Http Log Failed");
                }

            } catch (Exception e) {
                log.error("### save request log failed, RequestEntity: {}",
                        requestEntity, e);
            }
        });
    }*/
}
